home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / programming / source / fbm12s.lha / flgifw.c < prev    next >
C/C++ Source or Header  |  1994-07-18  |  4KB  |  137 lines

  1. /*****************************************************************
  2.  * flgifw.c: FBM Release 1.0 25-Feb-90 Michael Mauldin
  3.  *
  4.  * Modifications to GIFTORLE are Copyright (C) 1989,1990 by Michael
  5.  * Mauldin.  Permission is granted to use this file in whole or in
  6.  * part for any purpose, educational, recreational or commercial,
  7.  * provided that this copyright notice is retained unchanged.
  8.  * This software is available to all free of charge by anonymous
  9.  * FTP and in the UUNET archives.
  10.  *
  11.  * CONTENTS
  12.  *    write_gif (image, stream, mstr, mlen)
  13.  *
  14.  * EDITLOG
  15.  *    LastEditDate = Mon Jun 25 00:09:49 1990 - Michael Mauldin
  16.  *    LastFileName = /usr2/mlm/src/misc/fbm/flgifc.c
  17.  *
  18.  * HISTORY
  19.  * 25-Jun-90  Michael Mauldin (mlm@cs.cmu.edu) Carnegie Mellon
  20.  *    Package for Release 1.0
  21.  *
  22.  * 07-Mar-89  Michael Mauldin (mlm) at Carnegie Mellon University
  23.  *    Beta release (version 0.9) mlm@cs.cmu.edu
  24.  *
  25.  * 19-Feb-89  Michael Mauldin (mlm) at Carnegie Mellon University
  26.  *    Adapted to FBM package.
  27.  *
  28.  * 13-Feb-89  David Rowley (mgardi@watdcsu.waterloo.edu)
  29.  *    GIF encoding modifications.
  30.  *
  31.  *    Based on: compress.c - File compression ala IEEE Computer, June 1984.
  32.  *
  33.  *    Spencer W. Thomas       (decvax!harpo!utah-cs!utah-gr!thomas)
  34.  *    Jim McKie               (decvax!mcvax!jim)
  35.  *    Steve Davies            (decvax!vax135!petsd!peora!srd)
  36.  *    Ken Turkowski           (decvax!decwrl!turtlevax!ken)
  37.  *    James A. Woods          (decvax!ihnp4!ames!jaw)
  38.  *    Joe Orost               (decvax!vax135!petsd!joe)
  39.  *
  40.  *****************************************************************/
  41.  
  42. # include <stdio.h>
  43. # include "fbm.h"
  44.  
  45. unsigned char *pixels = NULL;
  46. int rowlen = 0;
  47.  
  48. int GetGIFPixel ();
  49.  
  50. #ifndef lint
  51. static char *fbmid =
  52. "$FBM flgifw.c <1.0> 25-Jun-90  (C) 1989,1990 by Michael Mauldin, source \
  53. code available free from MLM@CS.CMU.EDU and from UUNET archives$";
  54. #endif
  55.  
  56. write_gif (image, wfile)
  57. FBM *image;
  58. FILE *wfile;
  59. { register int i, bits, sum, bkg, clrs;
  60.   unsigned char *red, *grn, *blu;
  61.   int rint[256], gint[256], bint[256];
  62.     
  63.   if (image->hdr.planes > 1 || image->hdr.clrlen == 0)
  64.   { fprintf (stderr, "write_gif can only handle mapped color images\n");
  65.     return (0);
  66.   }
  67.  
  68.   clrs = image->hdr.clrlen / 3;
  69.   
  70.   /* Calculate bits per pixel in colormap */
  71.   for (i=clrs, bits=1; i > 2; )
  72.   { i >>= 1; bits++; }
  73.  
  74.   if (1 << bits != clrs)
  75.   { fprintf (stderr, "Error, number of colors %d is not a power of 2\n",
  76.          clrs);
  77.     return (0);
  78.   }
  79.   
  80.   if (bits < 1 || bits > 8)
  81.   { fprintf (stderr, "Error, bits per pixel (%d) must be in range 1..8\n",
  82.          bits);
  83.     return (0);
  84.   }
  85.   
  86.   red = image->cm;
  87.   grn = red + clrs;
  88.   blu = grn + clrs;
  89.  
  90.   pixels = image->bm;
  91.   rowlen = image->hdr.rowlen;
  92.   
  93.   /* Copy colormap, and find darkest pixel for background */
  94.   { bkg = 0; sum = 1e9;
  95.  
  96.     for (i=0; i<clrs; i++)
  97.     { rint[i] = red[i];
  98.       gint[i] = grn[i];
  99.       bint[i] = blu[i];
  100.  
  101.       if (red[i] + grn[i] + blu[i] < sum)
  102.       { bkg = i; sum = red[i] + grn[i] + blu[i]; }
  103.     }
  104.   }
  105.  
  106.   fprintf (stderr, "Writing GIF file [%dx%d], %d colors, %d bits, bkg %d\n",
  107.       image->hdr.cols, image->hdr.rows, clrs, bits, bkg);
  108.  
  109. # ifdef DEBUG
  110.   fprintf (stderr, "\n\nColormap:\n");
  111.   for (i=0; i<clrs; i++)
  112.   { fprintf (stderr, "%5d: <%3d, %3d, %3d>\n",
  113.          i, rint[i], gint[i], bint[i]);
  114.   }
  115. # endif
  116.  
  117.   return (GIFEncode (
  118.           wfile,
  119.           image->hdr.cols,        /* width */
  120.           image->hdr.rows,        /* height */
  121.           0,            /* No interlacing */
  122.           bkg,            /* Index of Backgrounf */
  123.           bits,            /* Bits Per pixel */
  124.           rint,            /* Red colormap */
  125.           gint,            /* Green colormap */
  126.           bint,            /* Blue colormap */
  127.           GetGIFPixel ) );        /* Get Pixel value */
  128. }
  129.  
  130. /* Returns value of next pixel */
  131.  
  132. GetGIFPixel (x, y)
  133. int x, y;
  134. {
  135.   return (pixels[y * rowlen + x]);
  136. }
  137.